/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ar.service;

import java.util.List;

import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.payment.dao.TapDao;
import com.jeesite.modules.payment.entity.Tap;
import com.jeesite.modules.payment.entity.TapDetail;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.ar.entity.Tar;
import com.jeesite.modules.ar.dao.TarDao;
import com.jeesite.modules.ar.entity.TarDetail;
import com.jeesite.modules.ar.dao.TarDetailDao;

/**
 * 应收单Service
 * @author YY
 * @version 2021-03-12
 */
@Service
@Transactional(readOnly=true)
public class TarService extends CrudService<TarDao, Tar> {
	@Autowired
	private TarDao tarDao;
	@Autowired
	private TarDetailDao tarDetailDao;
	@Autowired
	private CodeRuleService codeRuleService;
	/**
	 * 获取单条数据
	 * @param tar
	 * @return
	 */
	@Override
	public Tar get(Tar tar) {
		Tar entity = super.get(tar);
		if (entity != null){
			TarDetail tarDetail = new TarDetail(entity);
			tarDetail.setStatus(TarDetail.STATUS_NORMAL);
			entity.setTarDetailList(tarDetailDao.findList(tarDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param tar 查询条件

	 * @return
	 */
	@Override
	public Page<Tar> findPage(Tar tar) {
		return super.findPage(tar);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param tar
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Tar tar) {
		if(tar.getIsNewRecord()){
			tar.setArCode(codeRuleService.getCode("tar001"));
			tar.setCreateBy(UserUtils.getUser().getLoginCode());
			tar.setBillStatus("1");
		}
		super.save(tar);

		if (tar.getIsNewRecord()){
			codeRuleService.updateCurrentNumber("tar001");
		}
		// 保存 Tar子表
		for (TarDetail tarDetail : tar.getTarDetailList()){
			if (!TarDetail.STATUS_DELETE.equals(tarDetail.getStatus())){
				tarDetail.setArId(tar);
				if (tarDetail.getIsNewRecord()){
					tarDetail.setReiLeft(tarDetail.getApAmount());
					tarDetailDao.insert(tarDetail);
				}else{
					tarDetailDao.update(tarDetail);
				}
			}else{
				tarDetailDao.delete(tarDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param tar
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Tar tar) {
		super.updateStatus(tar);
	}
	
	/**
	 * 删除数据
	 * @param tar
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Tar tar) {
		super.delete(tar);
		TarDetail tarDetail = new TarDetail();
		tarDetail.setArId(tar);
		tarDetailDao.deleteByEntity(tarDetail);
	}
	/**
	 * 获取一个应收详情
	 * @param id
	 * @return
	 */

	@Transactional(readOnly=false)
	public TarDetail findById(String id){
		TarDetail tarDetail = tarDetailDao.findById(id);
		return tarDetail;
	}

	/**
	 * 获取一个应收单号
	 * @param id
	 * @return
	 */

	@Transactional(readOnly=false)
	public Tar selectOne(String id){
		Tar tar= tarDao.selectOne(id);
		return tar;
	}
}